import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { AlarmRecordModel } from '#/api/equipment/models';
import type { DictModel } from '#/api/system/models';

import { ref } from 'vue';

import dayjs from 'dayjs';

import { useAlarmLevelOptions } from '#/api/equipment/models';
import { getLineListApi } from '#/api/master/line';
import { getDictByCodeApi } from '#/api/system/dict';

const rangePresets = ref([
  { label: '最近 7 天', value: [dayjs().add(-7, 'd'), dayjs()] },
  { label: '最近 14 天', value: [dayjs().add(-14, 'd'), dayjs()] },
  { label: '最近 30 天', value: [dayjs().add(-30, 'd'), dayjs()] },
  { label: '最近 90 天', value: [dayjs().add(-90, 'd'), dayjs()] },
]);

/** 获取表格查询表单的字段配置。 */
export function useSearchSchema(): VbenFormSchema[] {
  return [
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        labelField: 'lineName',
        valueField: 'lineCode',
        api: getLineListApi,
        immediate: false,
      },
      fieldName: 'lineCode',
      label: '生产线',
    },
    {
      component: 'Input',
      componentProps: {
        allowClear: true,
      },
      fieldName: 'workCenter',
      label: '工作中心',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        showSearch: true,
        optionFilterProp: 'label',
        afterFetch: (data: DictModel) => {
          return data.items.map((item) => ({
            label: item.label,
            value: item.code,
          }));
        },
        api: getDictByCodeApi,
        params: { code: 'mes_alarm_category' },
        immediate: false,
      },
      fieldName: 'category',
      label: '报警分类',
    },
    {
      component: 'RangePicker',
      componentProps: {
        presets: rangePresets,
      },
      fieldName: 'rangeDate',
      label: '报警开始日期',
    },
  ];
}

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<AlarmRecordModel>,
): VxeTableGridOptions<AlarmRecordModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'lineCode', title: '产线代码', width: 120 },
    { field: 'workCenter', title: '工作中心', width: 120 },
    { field: 'index', title: '顺序号', width: 80 },
    { field: 'content', title: '警报内容', minWidth: 180 },
    { field: 'category', title: '警报分类', width: 140 },
    {
      field: 'alarmLevel',
      title: '警报级别',
      width: 80,
      cellRender: {
        name: 'CellTag',
        options: useAlarmLevelOptions(),
      },
    },
    {
      field: 'status',
      title: '警报状态',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: [
          { color: 'warning', label: '报警中', value: 1 },
          { color: 'blue', label: '已结束', value: 2 },
        ],
      },
    },
    { field: 'startTime', title: '开始时间', width: 150 },
    { field: 'endTime', title: '结束时间', width: 150 },
    { field: 'duration', title: '时长 (分钟)', width: 120 },
    {
      cellRender: {
        attrs: {
          nameField: 'index',
          nameTitle: '警报定义',
          onClick: onActionClick,
        },
        props: {
          disabled: (params: any) => params.status === 2,
        },
        name: 'CellOperation',
        options: [{ code: 'close', text: '关闭', confirm: true }],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 60,
    },
  ];
}
